<template>
  <div>
    <h1>{{id?'编辑':'新建'}}英雄 </h1>
    <el-form @submit.native.prevent="save" label-width="120px" label-position="right">
      <el-tabs type="border-card" value="skills">
        <el-tab-pane label="基础信息">
          <el-form-item label="名称" >
            <el-input v-model="model.name" placeholder=""></el-input>
          </el-form-item>
          <el-form-item label="称号" >
            <el-input v-model="model.title" placeholder=""></el-input>
          </el-form-item>
          <el-form-item label="头像" >
            <el-upload
              class="avatar-uploader"
              :action="uploadUrl"
              :show-file-list="false"
              :headers="getAuthorization()"
              :on-success="afterUploads">
              <img v-if="model.avatar" :src="model.avatar" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="类型" >
            <el-select v-model="model.categories" multiple >
              <el-option v-for="item in categories" :key="item._id" :label="item.name" :value="item._id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="难度" >
            <el-rate :max="9" show-score style="margin-top:0.6rem" v-model="model.scores.difficult" placeholder=""></el-rate>
          </el-form-item>
          <el-form-item label="技能" >
            <el-rate :max="9" show-score style="margin-top:0.6rem" v-model="model.scores.skills" placeholder=""></el-rate>
          </el-form-item>
          <el-form-item label="攻击" >
            <el-rate :max="9" show-score style="margin-top:0.6rem" v-model="model.scores.attack" placeholder=""></el-rate>
          </el-form-item>
          <el-form-item label="生存" >
            <el-rate :max="9" show-score style="margin-top:0.6rem" v-model="model.scores.survive" placeholder=""></el-rate>
          </el-form-item>
          <el-form-item label="顺风出装">
            <el-select v-model="model.items1" multiple >
              <el-option v-for="item in items" :key="item._id" :value="item._id" :label="item.name"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="逆风出装">
            <el-select v-model="model.items2" multiple >
              <el-option v-for="item in items" :key="item._id" :value="item._id" :label="item.name"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="使用技巧" >
            <el-input v-model="model.usageTips" placeholder="使用技巧" type="textxarea"></el-input>
          </el-form-item>
          <el-form-item label="对抗技巧" >
            <el-input v-model="model.battleTips" placeholder="对抗技巧" type="textxarea"></el-input>
          </el-form-item>
          <el-form-item label="团战思路" >
            <el-input v-model="model.teamTips" placeholder="团战思路" type="textxarea"></el-input>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="技能">
          <el-button type="text" @click="model.skills.push({})"><i class="el-icon-plus"></i>添加技能</el-button>
          <el-row type="flex" style="flex-wrap:wrap">
            <el-col :span="12" v-for="(item,index) in model.skills" :key="index">
              <el-form-item label="名称">
                <el-input placeholder="" v-model="item.name"></el-input>
              </el-form-item>
              <el-form-item label="图标">
                <el-upload
                  class="avatar-uploader"
                  :action="$http.defaults.baseURL+'/upload'"
                  :show-file-list="false"
                  :on-success="res => $set(item, 'icon', res.url)">
                  <img v-if="item.icon" :src="item.icon" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item label="描述">
                <el-input v-model="item.description" type="textarea" placeholder=""></el-input>
              </el-form-item>
              <el-form-item label="小提示">
                <el-input v-model="item.tips" type="textarea" placeholder=""></el-input>
              </el-form-item>
              <el-form-item >
                <el-button type="danger" @click="model.skills.splice(index,1)">删除</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
      <el-form-item style="margin-top:1rem" >
        <el-button  type="primary" native-type="submit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 表单数据
      model: {
        name: '',
        avatar: '',
        scores: {
          difficult: 0
        },
        skills: [
          { icon: '' }
        ]
      },
      // 分类
      categories: [],
      // 装备信息
      items: []
    }
  },
  props: {
    id: {}
  },
  methods: {
    // 点击按钮提交
    async save () {
      let res
      if (this.id) {
        // 编辑提交
        res = await this.$http.put(`rest/heroes/${this.id}`, this.model)
      } else {
        // 新增提交
        res = await this.$http.post('rest/heroes', this.model)
      }
      console.log(res)
      // 注意这里是前端页面路径，不需要加rest
      this.$router.push('/heroes/list')
      this.$message.success('保存成功')
    },
    // 根据id获取编辑分类的信息
    async fetch () {
      const res = await this.$http.get(`rest/heroes/${this.id}`)
      // this.model = res.data
      // console.log(res.data)
      // 防止socres被覆盖，因为res.data没有scores，所以上面的difficult会报错，显示这个没有
      this.model = Object.assign({}, this.model, res.data)
    },
    // 根据id获取编辑分类的信息
    async fetchCategories () {
      const res = await this.$http.get('rest/categories')
      this.categories = res.data
    },
    // 头像图片点击上传
    afterUploads (res) {
      // console.log(res)
      // 基于vue双向数据绑定的问题，有时候如果这个属性没有事先赋值，需要采用这种赋值属性的方式
      // this.$set(this.model, 'icon', res.url)
      this.model.avatar = res.url
    },
    // 获取装备信息
    async getItems () {
      const res = await this.$http.get('/rest/items')
      this.items = res.data
      console.log(this.items)
    }
  },
  created () {
    this.fetchCategories()
    this.id && this.fetch()
    this.getItems()
  }
}
</script>

<style>

</style>
